package com.winit.openapi.mqx.send;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.winit.mqx.MessageType;
import com.winit.mqx.eventtemplate.DefaultEventTemplate;
import com.winit.mqx.vo.tp.KuaiDi100TrackingMessage;
import com.winit.openapi.mqx.KuaiDi100TrackingMqxSender;
import com.winit.openapi.mqx.validator.KuaiDi100TrackingMessageValidator;
import com.winit.openapi.mqx.vo.SyncResult;
import com.winit.openapi.mqx.vo.ValidatorResult;

/**
 * 快递100回调轨迹 Mqx 消息发送接口类
 * 
 * @version <pre>
 * Author   Version     Date        Changes
 * Winit    1.0         2015年10月29日     Created
 *
 * </pre>
 * @since 1.
 */
@Service("kuaiDi100TrackingMqxSender")
public class kuaiDi100TrackingMqxSenderImpl implements KuaiDi100TrackingMqxSender {

    private Logger logger = LoggerFactory.getLogger(kuaiDi100TrackingMqxSenderImpl.class);

    @Override
    public SyncResult exec(KuaiDi100TrackingMessage message) {
        SyncResult syncResult = new SyncResult();
        MessageType messageType = MessageType.CREATE_KUAIDI100_TRACKING;
        try {
            // 消息体参数校验
            ValidatorResult result = KuaiDi100TrackingMessageValidator.validate(message);
            if (ValidatorResult.VALIDATE_RESULT_CODE_FAILURE.equals(result.getCode())) {
                // TODO 持久化处理失败的消息
                logger.error("kuaiDi100TrackingMqxSenderImpl -> exec,发送【创建快递100轨迹】MQ消息失败,message type：{},msg:{}",
                    new Object[] { messageType.getName(), message });
                syncResult.setCode(SyncResult.SYNC_RESULT_CODE_FAILURE);
                syncResult.setMsg(result.getMsg());
                return syncResult;
            }

            DefaultEventTemplate.newInstance(messageType.getName()).send(message);
        } catch (Exception e) {
            logger.error("kuaiDi100TrackingMqxSenderImpl.exec 发送【创建快递100轨迹】MQ消息失败 message type：{},msg:{},error:{}",
                new Object[] { messageType.getName(), message, e });
            syncResult.setCode(SyncResult.SYNC_RESULT_CODE_FAILURE);
            syncResult.setMsg(e.getLocalizedMessage());
        }
        syncResult.setCode(SyncResult.SYNC_RESULT_CODE_SUCCESS);
        syncResult.setMsg("SUCCESS");
        return syncResult;
    }

}
